#!/bin/bash
# 日志重定向写入
HOST_LIST="10.0.0.7 10.0.0.8"
CMD_NUM=0 #执行的次数
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u321-linux-x64.tar.gz"
ZK_NAME="apache-zookeeper-3.7.0-bin.tar.gz"
SCALA_NAME="scala-2.12.11.tgz"
KAFKA_NAME="kafka_2.12-2.6.1.tgz"
# 删除日志文件
if [ -e ./deploy_kafka.log ]; then
    rm -f ./deploy_kafka.log
fi
exec 1>>./deploy_kafka.log 2>&1

# 多主机执行指令函数封装
function remote_execute() { #ssh连接主机后的操作服务器的方法
    for host in $HOST_LIST; do
        CMD_NUM=$(expr $CMD_NUM + 1)
        echo "+++++++++++Execute Command < $@ > ON Host:$host +++++++++++"
        sshpass -proot ssh -o StrictHostKeyChecking=no root@$host "$@"
        if [ $? -eq '0' ]; then
            echo "$CMD_NUM Congratulation.Command < $@ > execute success"
        else
            echo "$CMD_NUM Sorry.Comand < $@ > execute failed"
        fi
    done
}
# 复制文件的操作
# 第一个参数是本地文件或目录，第二个参数为远程端主机目录
# 使用:eg  remote_transfer "./xx.txt" "/tmp"
function remote_transfer() {
    SRC_FILE=$1
    DST_DIR=$2
    if [ $# -lt 2 ]; then
        echo "Usage: $0 <file|dir> <des_dir>"
        exit 1
    fi
    # 判断第一个参数是否存在,如果不存在则直接退出并提示给用户
    if [ ! -e $SRC_FILE ]; then
        echo "ERROR - $SRC_FILE is not exist.Please check..."
        exit 1
    fi
    for host in $HOST_LIST; do
        #不存在远程目录就创建
        echo "+++++++++++Transfer File To Host:$host +++++++++++"
        sshpass -proot ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir -p $DST_DIR -p;fi"
        scp -r -o StrictHostKeyChecking=no $SRC_FILE root@$host:$DST_DIR/
        if [ $? -eq 0 ]; then
            echo "Remote Host:$host  - info  -SCP  $SRC_FILE  To dir $DST_DIR Success"
        else
            echo "Remote Host:$host  - info  -SCP  $SRC_FILE  To dir $DST_DIR Failed"
        fi
    done
}
# 第一步：关闭firewalld和selinux
# systemctl status firewalld查看防火墙状态
# remote_execute "systemctl stop firewalld"
# remote_execute "systemctl disable firewalld"
# remote_execute "setenforce 0"
# remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

# 第二步：安装配置JDK
remote_transfer $LOCAL_DIR/$JDK_NAME $PACKAGE_DIR
remote_execute "if [ ! -d $APP_DIR ];then mkdir -p $APP_DIR; fi"
remote_execute "tar zxvf $PACKAGE_DIR/$JDK_NAME -C $APP_DIR"

cat >$LOCAL_DIR/java.sh <<EOF
export JAVA_HOME=/opt/source/jdk1.8.0_321
export PATH=\$PATH:\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin
export JAVA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/java.sh /etc/profile.d/
remote_execute "source /etc/profile.d/java.sh"
remote_execute "java -version"

# 第三步：安装配置zookeeper,并启动服务

## 管理主机传输zookeeper安装包到三节点
## 解压安装包
remote_transfer $LOCAL_DIR/$ZK_NAME $PACKAGE_DIR
remote_execute "tar -zxvf $PACKAGE_DIR/$ZK_NAME -C $APP_DIR"
## 创建zookeeper软连接
remote_execute "if [ -e $APP_DIR/zookeeper ];then rm -f $APP_DIR/zookeeper;fi"
remote_execute "ln -sv  $APP_DIR/apache-zookeeper-3.7.0-bin  $APP_DIR/zookeeper"

## 修改zookeeper配置文件
remote_execute "cp $APP_DIR/zookeeper/conf/zoo_sample.cfg $APP_DIR/zookeeper/conf/zoo.cfg" #样例配置文件更名
cat >$LOCAL_DIR/zoo_tmp.conf <<EOF
server.1=10.0.0.7:2888:3888  
server.1=10.0.0.9:2888:3888
EOF

remote_transfer $LOCAL_DIR/zoo_tmp.conf /tmp
remote_execute "cat /tmp/zoo_tmp.conf >> $APP_DIR/zookeeper/conf/zoo.cfg"

remote_execute "if [ -e /data/zk ];then rm -rf /data/zk;fi"
remote_execute "mkdir -p /data/zk" #创建zookeeper数据目录

remote_execute "sed -i 's/dataDir=\/tmp\/zookeeper/dataDir=\/data\/zk/g' $APP_DIR/zookeeper/conf/zoo.cfg" #配置文件数据目录更改
#数据目录生成唯一的myid文件(node1是集群控电脑的hostname的配置,hostname node01进行设置)
remote_execute 'if [ $(hostname) == "node01" ];then echo 1 >/data/zk/myid;fi'
remote_execute 'if [ $(hostname) == "node02" ];then echo 2 >/data/zk/myid;fi'

## 启动服务并验证

remote_execute "jps | grep  QuorumPeerMain | grep -v grep | awk '{print \$1}' > /tmp/zk.pid"
remote_execute "if [ -s /tmp/zk.pid ];then kill -9 \`cat /tmp/zk.pid\`; fi"
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh start"

# 安装scala环境
#
remote_transfer $LOCAL_DIR/$SCALA_NAME $PACKAGE_DIR
remote_execute "tar -zxvf  $PACKAGE_DIR/$SCALA_NAME -C $APP_DIR"

cat >$LOCAL_DIR/scala.sh <<EOF
export SCALA_HOME=$APP_DIR/scala-2.12.11
export PATH=\$PATH:\$SCALA_HOME/bin
export SCALA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/scala.sh /etc/profile.d
remote_execute "source /etc/profile.d/scala.sh"
remote_execute "scala -version"

# 安装配置kafka并启动服务代码实现
## 管理主机(/opt/tmp)传输kafka安装包到所有远端生产主机/opt/package
remote_transfer $LOCAL_DIR/$KAFKA_NAME $PACKAGE_DIR
## 所有远端生产主机(/opt/package)解压缩kafka安装包到安装目录(/opt/source)
remote_execute "tar -zxvf $PACKAGE_DIR/$KAFKA_NAME -C $APP_DIR"
## 创建kafka 2.12-2.6.1目录软连接，创建之前先判断软连接是否存在，如果存在则删除
remote_execute "if [ -e $APP_DIR/kafka ] ;then rm -rf $APP_DIR/kafka;fi"
remote_execute "ln -sv $APP_DIR/kafka_2.12-2.6.1 $APP_DIR/kafka"
## 创建kafka数据库目录：创建之前先判断改目录是否存在，如果存在则删除
remote_execute "if [ -e /data/kafka/log ];then rm -rf /data/kafka/log;fi"
remote_execute "mkdir -p /data/kafka/log"
## 修改kafka配置文件(/opt/source/kafka/config/server.properties)
### 修改内容
remote_execute "sed -i '/zookeeper.connect=localhost:2181/d' $APP_DIR/kafka/config/server.properties"
remote_execute "sed -i '\$azookeeper.connect=10.0.0.7:2181,10.0.0.8:2181' $APP_DIR/kafka/config/server.properties"

remote_execute "if [ \`hostname\`=='node01' ];then sed -i 's/broker.id=0/broker.id=100/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\`=='node02' ];then sed -i 's/broker.id=0/broker.id=101/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\`=='node02' ];then sed -i '\$alisteners=PLAINTEXT://10.0.0.7:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\`=='node02' ];then sed -i '\$alisteners=PLAINTEXT://10.0.0.8:9092' $APP_DIR/kafka/config/server.properties;fi"

remote_execute "sed -i 's/log.dirs=\/tmp\/kafka-logs/log.dirs=\/data\/kafka\/log/g' $APP_DIR/kafka/config/server.properties"
remote_execute "jps | grep Kafka | grep -v grep | awk '{print \$1}' > /tmp/kafka.pid"
remote_execute "if [ -s /tmp/kafka.pid ];then kill -9 \`cat /tmp/kafka.pid\`;fi"
remote_execute "$APP_DIR/kafka/bin/kafka-server-start.sh -daemon $APP_DIR/kafka/config/server.properties"

sleep 30

remote_execute "if [ \`hostname\` == 'node01' ];then $APP_DIR/kafka/bin/kafka-topics.sh --create --zookeeper localhost  --replication-factor 2 --partitions 5 --topic my-topic;fi "
sleep 5

remote_execute "if [ \`hostname\` == 'node01' ];then $APP_DIR/kafka/bin/kafka-topics.sh --describe --zookeeper localhost --topic my-topic;fi"
